var pageSize01;
var user001 = sessionStorage.getItem("user001");
$(function () {
        $.ajax({
            url: "/market/customer/customList",      //请求接口的地址
            type: "GET",
            data: {
                "servicePerson": user001,
            },
            dataType: "json",
            success: function (res) {
                $("#tbList").html("")
                var uui = 0;
                var dd01 = res;

                for (var i in dd01.data) {
                    var tr1 = "<tr></tr>";
                    $("#tbList").append(tr1);
                    var li = $("#tbList tr").eq(i);
                    uui += 1;
                    var str1 = "<td>" + uui + "</td>";
                    str1 += "<td id='customerName10'>" + dd01.data[i].customname + "</td>";
                    str1 += "<td>" + dd01.data[i].sex + "</td>";
                    str1 += "<td>" + dd01.data[i].telephone + "</td>";
                    str1 += "<td>" + dd01.data[i].company + "</td>";
                    str1 += "<td><span>" + dd01.data[i].pname + "" + dd01.data[i].cname + "</span></td>";
                    if (dd01.data[i].distractime1=="null" || dd01.data[i].distractime1==null) {
                        str1 += "<td></td>";
                        str1 += "<td></td>";
                    }else {
                        str1 += "<td>" + dd01.data[i].distractime1 + "</td>";
                        str1 += "<td>" + dd01.data[i].servicePerson + "</td>";
                    }

                    str1 += '<td><a href="customerView.html" class="tablelink">查看详情</a> </td>';
                    li.prepend(str1);
                }
                cliInfom0()

                $('.blue').eq(0).text(res.totalRows)
                $('.blue').eq(1).text(res.pageNum)
                // 分页
                var total= Number(res.totalRows)
                var pageSize= Number(res.pageSize)
                var pages=total/pageSize;
                if (total%pageSize!=0){
                    pages+=1;
                }
                pageSize01 =parseInt( pages)

                var pageStr='<li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>'
                for (var i=1;i<=pages;i++){
                    if (i == res.totalPage) {
                        pageStr += ' <li class="paginItem current"><a href="javascript:;">' + i + '</a></li>'
                    } else {
                        pageStr += '<li class="paginItem"><a href="javascript:;">' + i + '</a></li>'
                    }
                    if (i==pages-2){
                        pageStr+='<li class="paginItem more"><a href="javascript:;">...</a></li>'
                        pageStr+='<li class="paginItem"><a href="javascript:;">'+pages+'</a></li>'
                        break;
                    }
                }
                pageStr+='<li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>'
                $('.paginList').html(pageStr)

                pageOnclik0101()

            }, error: function (r) {
                alert("!!!")
            }
        })
    selectMoreCondition()
    cliInfom0()
    onclPro()
    checkCustomName()
    checkCompanyName()
    pageOnclik0101()

})

// 根据省确定城市
function onclPro() {
    $("#province").blur(function () {
        // var a = $("#province").children("#orderid01").html();
        var prov01 = $("#province option:selected").text();
        $.ajax({
            url: "/cityByProvince1/" + prov01,      //请求接口的地址
            type: "GET",
            success: function (res) {

                $("#city1").html("")
                var st1 = " <option>请选择城市</option>";
                $("#city1").append(st1);
                for (var i in res) {
                    // alert(res[i].cname)
                    var tr1 = "<option>" + res[i].cname + "</option>";
                    $("#city1").append(tr1);

                }
            }
        })

    });
}

//正则，验证名字
function checkCustomName() {
    $("#customerName").blur(function () {
        var str = $("#customerName").val();
        var han = /^[\u4e00-\u9fa5]+$/;
        if (str==''){
            return false;
        }
        if (!han.test(str)) {
            alert("请输入用户名");
            var str = $("#customerName").val("");
            return false;
        } else {
            return true;
        }
        ;
    })
}

//正则，验证公司名
function checkCompanyName() {
    $("#companyName").blur(function () {
        var str = $("#companyName").val();
        var han = /^[\u4e00-\u9fa5]+$/;
        if (str==''){
            return false;
        }
        if (!han.test(str)) {
            alert("请输入公司名");
            var str = $("#companyName").val("");
            return false;
        } else {
            return true;
        }
        ;
    })
}

//点击详情
function cliInfom0() {
    $(".tablelink").click(function () {
        var b01 = $(this).parent().parent().children("#customerName10").text();
        // var b001 = $(this).parent().parent().children("#cusname01").text();
        sessionStorage.removeItem("OneName01");
        sessionStorage.setItem("OneName01", JSON.stringify(b01));
    })
}

//查询
function selectMoreCondition() {
    $("#selectMoreCondition").click(function () {
        var company01 = $("#companyName").val();
        var customer01 = $("#customerName").val();
        var province = $("#province option:selected").text();
        var city1 = $("#city1 option:selected").text();

        if (province == "请选择省份") {
            province = ''
        }
        if (city1 == "请选择城市") {
            city1 = ''
        }
        // var status001 = $("#status01 option:selected").val();
        // if (status001 == "请选择") {
        //     status001 = ''
        // }
        // var distribute001 = $("#distribute01 option:selected").val();
        // if (distribute001 == "请选择") {
        //     distribute001 = ''
        // }
        if (  province == '' && company01 == '' && customer01 == '') {
            alert("请选择查询条件")
        }

        $.ajax({
            url: "/market/customer/getMoreCondition",      //请求接口的地址
            type: "GET",
            data: {
                "company": company01,
                "customname": customer01,
                "pname": province,
                "cname": city1,
            },
            dataType: "json",
            success: function (res) {
                $("#tbList").html("")
                var uui = 0;
                var dd01 = res;
                for (var i in dd01.data) {
                    var tr1 = "<tr></tr>";
                    $("#tbList").append(tr1);
                    var li = $("#tbList tr").eq(i);
                    uui += 1;
                    var str1 = "<td>" + uui + "</td>";
                    str1 += "<td id='customerName10'>" + dd01.data[i].customname + "</td>";
                    str1 += "<td>" + dd01.data[i].sex + "</td>";
                    str1 += "<td>" + dd01.data[i].telephone + "</td>";
                    str1 += "<td>" + dd01.data[i].company + "</td>";
                    str1 += "<td><span>" + dd01.data[i].pname + "" + dd01.data[i].cname + "</span></td>";

                    if (dd01.data[i].distractime1=="null" || dd01.data[i].distractime1==null) {
                        str1 += "<td></td>";
                        str1 += "<td></td>";
                    }else {
                        str1 += "<td>" + dd01.data[i].distractime1 + "</td>";
                        str1 += "<td>" + dd01.data[i].servicePerson + "</td>";
                    }

                    str1 += '<td><a href="customerView.html" class="tablelink">查看详情</a> </td>';
                    li.prepend(str1);
                }
                cliInfom0()
                if (res.totalRows==0){
                    alert("没用相关数据")
                    $('.blue').eq(0).text(0)
                    $('.blue').eq(1).text(1)
                }else {
                    $('.blue').eq(0).text(res.totalRows)
                    $('.blue').eq(1).text(res.pageNum)
                }
                // 分页
                var total= Number(res.totalRows)
                var pageSize= Number(res.pageSize)
                var pages=total/pageSize;
                if (total%pageSize!=0){
                    pages+=1;
                }
                pageSize01 = pages;
                var pageStr='<li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>'
                for (var i=1;i<=pages;i++){
                    if (i == res.totalPage) {
                        pageStr += ' <li class="paginItem current"><a href="javascript:;">' + i + '</a></li>'
                    } else {
                        pageStr += '<li class="paginItem"><a href="javascript:;">' + i + '</a></li>'
                    }
                    if (i==pages-2){
                        pageStr+='<li class="paginItem more"><a href="javascript:;">...</a></li>'
                        pageStr+='<li class="paginItem"><a href="javascript:;">'+pages+'</a></li>'
                        break;
                    }
                }
                pageStr+='<li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>'
                $('.paginList').html(pageStr)

                pageOnclik0101()

            },
            error: function (r) {
                alert("!!!")
            }
        })
    })
}

//分页点击
function pageOnclik0101() {
    $(".paginItem").click(function () {
        var click = $(this).children("a").text();
        var current = $('.blue').eq(1).text();
        var totalRow = $('.blue').eq(0).text();
        var total = Number(totalRow)
        var pageSize = Number(3)
        var pages = total / pageSize;
        if (total % pageSize != 0) {
            pages += 1;
        }
        if (current == click) {
            $(this).children("a").addClass("more")
        } else {
            if (click == "...") {
            } else if (click == '') {
                if ('<a href="javascript:;"><span class="pagepre"></span></a>' == $(this).html()) {
                    if (current != 1) {
                        current -= 1;
                        $("#tbList").html("")
                        $.ajax({
                            url: "/market/customer/customList",      //请求接口的地址
                            type: "GET",
                            data: {
                                "pageNum": current,
                            },
                            dataType: "json",
                            success: function (res) {
                                $("#tbList").html("")
                                var uui = 0;
                                var dd01 = res;
                                for (var i in dd01.data) {
                                    var tr1 = "<tr></tr>";
                                    $("#tbList").append(tr1);
                                    var li = $("#tbList tr").eq(i);
                                    uui += 1;
                                    var str1 = "<td>" + uui + "</td>";
                                    str1 += "<td id='customerName10'>" + dd01.data[i].customname + "</td>";
                                    str1 += "<td>" + dd01.data[i].sex + "</td>";
                                    str1 += "<td>" + dd01.data[i].telephone + "</td>";
                                    str1 += "<td>" + dd01.data[i].company + "</td>";
                                    str1 += "<td><span>" + dd01.data[i].pname + "" + dd01.data[i].cname + "</span></td>";
                                    if (dd01.data[i].distractime1=="null" || dd01.data[i].distractime1==null) {
                                        str1 += "<td></td>";
                                        str1 += "<td></td>";
                                    }else {
                                        str1 += "<td>" + dd01.data[i].distractime1 + "</td>";
                                        str1 += "<td>" + dd01.data[i].servicePerson + "</td>";
                                    }

                                    str1 += '<td><a href="customerView.html" class="tablelink">查看详情</a> </td>';
                                    li.prepend(str1);
                                }
                                cliInfom0()
                                $('.blue').eq(0).text(res.totalRows)
                                $('.blue').eq(1).text(res.pageNum)
                                // 分页
                                var total= Number(res.totalRows)
                                var pageSize= Number(res.pageSize)
                                var pages=total/pageSize;
                                if (total%pageSize!=0){
                                    pages+=1;
                                }
                                pageSize01 =parseInt( pages)

                                var pageStr='<li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>'
                                if(pages>=3) {
                                    for (var i = 1; i <= pages; i++) {
                                        if (i == res.totalPage) {
                                            pageStr += '<li class="paginItem current more" id="current01"><a href="javascript:;">' + i + '</a></li>'
                                        } else {
                                            pageStr += '<li class="paginItem"><a href="javascript:;">' + i + '</a></li>'
                                        }
                                        if (i == pages - 2) {
                                            pageStr += '<li class="paginItem more"><a href="javascript:;">...</a></li>'
                                            pageStr += '<li class="paginItem"><a href="javascript:;">' + pages + '</a></li>'
                                            break;
                                        }
                                    }
                                }else {
                                    for (var i = 1; i <= pages; i++) {
                                        if (i == res.totalPage) {
                                            pageStr += '<li class="paginItem current more" id="current01"><a href="javascript:;">' + i + '</a></li>'
                                        } else {
                                            pageStr += '<li class="paginItem"><a href="javascript:;">' + i + '</a></li>'
                                        }
                                    }
                                }
                                pageStr+='<li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>'
                                $('.paginList').html(pageStr)

                                cliInfom0001()
                                selectMoreCondition()
                                pageOnclik0101()

                            }, error: function (r) {
                                alert("!!!")
                            }
                        })
                    }
                } else {
                    if (current != pageSize01) {
                        var currentInt=parseInt(current)+1;
                        $("#tbList").html("")
                        $.ajax({
                            url: "/market/customer/customList",      //请求接口的地址
                            type: "GET",
                            data: {
                                "pageNum": currentInt,
                            },
                            dataType: "json",
                            success: function (res) {
                                $("#tbList").html("")
                                var uui = 0;
                                var dd01 = res;
                                for (var i in dd01.data) {
                                    var tr1 = "<tr></tr>";
                                    $("#tbList").append(tr1);
                                    var li = $("#tbList tr").eq(i);
                                    uui += 1;
                                    var str1 = "<td>" + uui + "</td>";
                                    str1 += "<td id='customerName10'>" + dd01.data[i].customname + "</td>";
                                    str1 += "<td>" + dd01.data[i].sex + "</td>";
                                    str1 += "<td>" + dd01.data[i].telephone + "</td>";
                                    str1 += "<td>" + dd01.data[i].company + "</td>";
                                    str1 += "<td><span>" + dd01.data[i].pname + "" + dd01.data[i].cname + "</span></td>";
                                    if (dd01.data[i].distractime1=="null" || dd01.data[i].distractime1==null) {
                                        str1 += "<td></td>";
                                        str1 += "<td></td>";
                                    }else {
                                        str1 += "<td>" + dd01.data[i].distractime1 + "</td>";
                                        str1 += "<td>" + dd01.data[i].servicePerson + "</td>";
                                    }

                                    str1 += '<td><a href="customerView.html" class="tablelink">查看详情</a> </td>';
                                    li.prepend(str1);
                                }
                                cliInfom0()
                                $('.blue').eq(0).text(res.totalRows)
                                $('.blue').eq(1).text(res.pageNum)
                                // 分页
                                var total= Number(res.totalRows)
                                var pageSize= Number(res.pageSize)
                                var pages=total/pageSize;
                                if (total%pageSize!=0){
                                    pages+=1;
                                }
                                pageSize01 =parseInt( pages)

                                var pageStr='<li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>'
                                if(pages>=3) {
                                    for (var i = 1; i <= pages; i++) {
                                        if (i == res.totalPage) {
                                            pageStr += '<li class="paginItem current more" id="current01"><a href="javascript:;">' + i + '</a></li>'
                                        } else {
                                            pageStr += '<li class="paginItem"><a href="javascript:;">' + i + '</a></li>'
                                        }
                                        if (i == pages - 2) {
                                            pageStr += '<li class="paginItem more"><a href="javascript:;">...</a></li>'
                                            pageStr += '<li class="paginItem"><a href="javascript:;">' + pages + '</a></li>'
                                            break;
                                        }
                                    }
                                }else {
                                    for (var i = 1; i <= pages; i++) {
                                        if (i == res.totalPage) {
                                            pageStr += '<li class="paginItem current more" id="current01"><a href="javascript:;">' + i + '</a></li>'
                                        } else {
                                            pageStr += '<li class="paginItem"><a href="javascript:;">' + i + '</a></li>'
                                        }
                                    }
                                }
                                pageStr+='<li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>'
                                $('.paginList').html(pageStr)


                                selectMoreCondition()
                                pageOnclik0101()

                            }, error: function (r) {
                                alert("!!!")
                            }
                        })
                    }
                }


            } else {
                $("#tbList").html("")
                $("#tbList").html("")
                $.ajax({
                    url: "/market/customer/customList",      //请求接口的地址
                    type: "GET",
                    data: {
                        "pageNum": click,
                    },
                    dataType: "json",
                    success: function (res) {
                        $("#tbList").html("")
                        var uui = 0;
                        var dd01 = res;
                        for (var i in dd01.data) {
                            var tr1 = "<tr></tr>";
                            $("#tbList").append(tr1);
                            var li = $("#tbList tr").eq(i);
                            uui += 1;
                            var str1 = "<td>" + uui + "</td>";
                            str1 += "<td id='customerName10'>" + dd01.data[i].customname + "</td>";
                            str1 += "<td>" + dd01.data[i].sex + "</td>";
                            str1 += "<td>" + dd01.data[i].telephone + "</td>";
                            str1 += "<td>" + dd01.data[i].company + "</td>";
                            str1 += "<td><span>" + dd01.data[i].pname + "" + dd01.data[i].cname + "</span></td>";
                            if (dd01.data[i].distractime1=="null" || dd01.data[i].distractime1==null) {
                                str1 += "<td></td>";
                                str1 += "<td></td>";
                            }else {
                                str1 += "<td>" + dd01.data[i].distractime1 + "</td>";
                                str1 += "<td>" + dd01.data[i].servicePerson + "</td>";
                            }

                            str1 += '<td><a href="customerView.html" class="tablelink">查看详情</a> </td>';
                            li.prepend(str1);
                        }
                        cliInfom0()
                        $('.blue').eq(0).text(res.totalRows)
                        $('.blue').eq(1).text(res.pageNum)
                        // 分页
                        var total= Number(res.totalRows)
                        var pageSize= Number(res.pageSize)
                        var pages=total/pageSize;
                        if (total%pageSize!=0){
                            pages+=1;
                        }
                        pageSize01 =parseInt( pages)

                        var pageStr='<li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>'
                        if(pages>=3) {
                            for (var i = 1; i <= pages; i++) {
                                if (i == res.totalPage) {
                                    pageStr += '<li class="paginItem current more" id="current01"><a href="javascript:;">' + i + '</a></li>'
                                } else {
                                    pageStr += '<li class="paginItem"><a href="javascript:;">' + i + '</a></li>'
                                }
                                if (i == pages - 2) {
                                    pageStr += '<li class="paginItem more"><a href="javascript:;">...</a></li>'
                                    pageStr += '<li class="paginItem"><a href="javascript:;">' + pages + '</a></li>'
                                    break;
                                }
                            }
                        }else {
                            for (var i = 1; i <= pages; i++) {
                                if (i == res.totalPage) {
                                    pageStr += '<li class="paginItem current more" id="current01"><a href="javascript:;">' + i + '</a></li>'
                                } else {
                                    pageStr += '<li class="paginItem"><a href="javascript:;">' + i + '</a></li>'
                                }
                            }
                        }
                        pageStr+='<li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>'
                        $('.paginList').html(pageStr)


                        selectMoreCondition()
                        pageOnclik0101()

                    }, error: function (r) {
                        alert("!!!")
                    }
                })
            }
        }

    })
}





